package admin

import (
	"gitee.com/gopher2011/gin"
	"gitee.com/gopher2011/mall/app/dao"
	"gitee.com/gopher2011/mall/app/service"
	"net/http"
	"strconv"
)

// 首页新品管理 /home/new_product
var SmsHomeNewProduct = &smsHomeNewProductApi{}

type smsHomeNewProductApi struct{}

// 添加首页推荐品牌 /create Post dao.SmsHomeNewProductReq
func (it *smsHomeNewProductApi) Create(r *gin.Context) {
	// 将请求参数解析到 ptr上
	//var ptr []dao.SmsHomeNewProduct
	var ptr dao.SmsHomeNewProductReq
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsHomeNewProduct.Create(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "添加首页推荐品牌失败!"})
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "添加首页推荐品牌成功!", "data": res})
	}
}

// 修改推荐排序 /update/sort/:id Post id,sort
func (it *smsHomeNewProductApi) UpdateSort(r *gin.Context) {
	// 将请求参数解析到 ptr上
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	form := r.PostForm("sort")
	sort, _ := strconv.Atoi(form)
	res, err := service.SmsHomeNewProduct.UpdateSort(int64(id), int32(sort))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改推荐排序失败!"})
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "修改推荐排序成功!", "data": res})
	}
}

// 批量删除推荐 /delete Post ids[]int64
func (it *smsHomeNewProductApi) Delete(r *gin.Context) {
	list := r.PostFormArray("ids")
	ids := make([]int64, 0)
	for _, v := range list {
		id, _ := strconv.Atoi(v)
		ids = append(ids, int64(id))
	}
	res, err := service.SmsHomeNewProduct.Delete(ids)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "批量删除推荐失败!"})
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "批量删除推荐成功!", "data": res})
	}
}

//批量修改推荐状态 /update/recommend_status Post ids[]int64,recommend_status
func (it *smsHomeNewProductApi) UpdateRecommendStatus(r *gin.Context) {
	list := r.PostFormArray("ids")
	ids := make([]int64, 0)
	for _, v := range list {
		id, _ := strconv.Atoi(v)
		ids = append(ids, int64(id))
	}
	form := r.PostForm("recommend_status")
	status, _ := strconv.Atoi(form)
	res, err := service.SmsHomeNewProduct.UpdateRecommendStatus(ids, int32(status))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "批量修改推荐状态失败!"})
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "批量修改推荐状态成功!", "data": res})
	}
}

// 分页查询推荐 /list Get
func (it *smsHomeNewProductApi) List(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SmsHomeNewProductList
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsHomeNewProduct.List(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "分页查询推荐失败!"})
	} else {
		r.JSON(http.StatusOK, gin.H{"msg": "分页查询推荐成功!", "data": res})
	}
}
